<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:th="http://www.thymeleaf.org"
	xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">

<head>
<title>登陆界面</title>
<link rel="stylesheet" type="text/css" media="screen"
	th:href="@{/css/loginPage.css}" />
<script type="text/javascript" th:src="@{/js/jquery-3.2.1.js}"></script>
<script type="text/javascript" th:src="@{/js/jquery.form.js}"></script>
</head>
<body>
	<form id="loginPageForm">
		<table>
			<tbody>
				<!--<tr>-->
					<!--<td>逻辑系统id</td>-->
					<!--<td><input th:type="text" th:id="sysId"-->
						<!--th:name="sysId" th:value="1cab27def8fb4c0f9486dcf844b783c0" /></td>-->
					<!--<td><font color="red">*</font></td>-->
				<!--</tr>-->
				<tr>
					<td>账号</td>
					<td><input th:type="text" th:id="loginName"
						th:name="loginName" /></td>
					<td><font color="red">*</font></td>
				</tr>
				<tr>
					<td>密码</td>
					<td><input th:type="password" th:id="password"
						th:name="password" /></td>
					<td><font color="red">*</font></td>
				</tr>
				<tr>
					<td><input th:type="button" onclick="login()" th:value="确定" /></td>
					<td><input th:type="button" onclick="cancelLogin()"
						th:value="取消" /></td>
				</tr>
			</tbody>
		</table>
	</form>
</body>

<script type="application/javascript">


        $(document).keyup(function (event) {
            if(event.keyCode == 13){
                login();
            }
        });

        function cancelLogin() {
            $("#loginName").val("");
            $("#password").val("");
        }
        function login() {
            var loginPath = "oauth/token";
            var loginName = $("#loginName").val();
            var password = $("#password").val();
            var sysId = $("#sysId").val();
            if(loginName == ""){
                alert("账号不能为空");
                return false;
            }
            if(password == ""  ){
                alert("密码不能为空");
                return false;
            }
            var data = {
                    username: loginName,
                    password: password,
                    grant_type: "password",
                    sysId: sysId
              };
            var headers = {
                    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
                    "Authorization": "Basic d2ViX2FwcDo="
                };
            $.ajax({
                url:loginPath,
                type:"post",
                dataType:"json",
                contentType: "application/json",
                headers: headers,
                async: false,
                data:data,
                success:function callbackFun(data){
                    //解析json
                    if(data.access_token!=null){
                   		localStorage.setItem("jhi-authenticationtoken",data.access_token);
                        window.location.href ="api-doc?Authorization="+data.access_token;
                    }
                },
                error:function(data){
                		alert("登陆失败，用户名或密码错误！");
                }
            });
        }

</script>
</html>
